define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'auth/admin/index',
                    add_url: 'auth/admin/add',
                    edit_url: 'auth/admin/edit',
                    del_url: 'auth/admin/del',
                    multi_url: 'auth/admin/multi',
                }
            });

            var table = $("#table");

            //在表格内容渲染完成后回调的事件
            table.on('post-body.bs.table', function (e, json) {
                $("tbody tr[data-index]", this).each(function () {
                    if (parseInt($("td:eq(1)", this).text()) == Config.admin.id) {
                        $("input[type=checkbox]", this).prop("disabled", true);
                    }
                });
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        //{field: 'state', checkbox: true },
                        {field: 'sort', title: '序号', operate:false},
                        //{field: 'username', title: '姓名', operate:'LIKE'},
                        {field: 'nickname', title: '姓名', operate:false},
                        {field: 'mobile', title: __('手机号'), operate:'LIKE'},
                        {field: 'email', title: __('邮箱地址'), operate:'LIKE'},
                        {field: 'user_id', title: __("用户id"), operate:false},
                        //{field: 'org_cate_name', title: __('单位名称'), operate:false},
                        {field: 'groups_text', title: __('角色'), operate:false, formatter: Table.api.formatter.label},
                        {field: 'createtime', title: __('加入时间'), formatter: Table.api.formatter.datetime, operate: false, addclass: 'datetimerange', sortable: true},

                        {field: 'status', title: __("Status"), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (value, row, index) {
                                //if(row.id == Config.admin.id || row.operate == false){
                                //    return '';
                                //}
                                if(row.operate == false){
                                    return '';
                                }
                                return Controller.api.formatter.operate(value, row, index);
                            }}
                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Controller.api.formatter.operate}
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: false,
                commonSearch: false,
                search:false,
                onLoadSuccess:function(){
                    // 这里就是数据渲染结束后的回调函数
                    $(".btn-editone").data("area", ["100%","100%"]);
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        api: {
            formatter: {
                subnode: function (value, row, index) {
                    return '<a href="javascript:;" data-toggle="tooltip" title="' + __('Toggle sub menu') + '" data-id="' + row.id + '" data-pid="' + row.pid + '" class="btn btn-xs btn-success '+ (row.level == 3? 'btn-hidden' : '') + ' btn-node-sub">'+ (row.level == 3? '' : '<i class="fa fa-plus"></i>')+'</a>';
                },
                operate: function (value, row, index) {
                    return '<a href="/admin/auth/admin/edit/ids/' + row.id + '" class="btn btn-xs btn-success btn-editone" data-toggle="tooltip" title="" data-table-id="table" data-field-index="8" data-row-index="0" data-button-index="1" data-original-title="编辑"><i class="fa fa-pencil"></i></a>&nbsp;&nbsp;' +
                        '<a href="javascript:;" class="btn btn-xs btn-danger btn-delone '+ ( row.id == Config.admin.id? 'hide' : '')+'" data-toggle="tooltip" title="删除" data-table-id="table" data-field-index="10" data-row-index="' +index + '" data-button-index="5"><i class="fa fa-trash"></i></a>';
                },
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});